/*
 * Digest.java
 *
 * Created on 16 de mayo de 2006, 10:21 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package ig.util;

import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 *
 * @author jsanchez
 */
public class Digest {
    
    
        public static String encriptar(String cadena) {
            return Digest.encriptar("MD5",cadena);
        }
        
//        public static String encriptar(char[] cadena) {
//            return Digest.encriptar("MD5",cadena.);
//        }
        
    	public static String encriptar(String Modo, String cadena) {
		try {			
//			System.out.println ("Genarando Hashing...");			
//			float P=(new File(file).length()/100);
//			Float P1=new Float(P);
//			int Paso=P1.intValue();			
//						
//			DataInputStream br =new DataInputStream(new BufferedInputStream(new FileInputStream(file)));
//					
			MessageDigest M_hash = MessageDigest.getInstance(Modo);
			byte[] Hash = null;
//			try {				
//				do{				
					M_hash.update(cadena.getBytes());
//					Nb++;//cuenta los byte leidos
//					if(Nb==Av && Av<Paso*100){
//						Av=Av+Paso;
//						System.out.print(cont+" % \r");						
//						cont++;
//					}
//				}while(true);				
//			} catch (Exception e) {br.close();}
//			
			Hash = M_hash.digest();
			
			StringBuffer StrBuff = new StringBuffer();
			
			for (int i = 0; i < Hash.length; i++) {
				String s = Integer.toHexString(Hash[i]);
				int length = s.length();
				if (length >= 2)
					StrBuff.append(s.substring(length - 2, length));
				else {
					StrBuff.append("0");
					StrBuff.append(s);
				}
			}
			
			return StrBuff.toString();
			
		} catch (NoSuchAlgorithmException e) {
			System.out.println("Error en JCA");
			return null;
		} //catch (IOException e) {
//			System.out.println("No se pudo leer el archibo");
//			return null;
//		}
	} //end metodo generar
}
